package com.yxt.shecaiyun.config;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.JWTVerifier;

import java.util.Date;
import java.util.Map;



public class JwtUtil {
    private static final String SECRET_KEY = "!QAZ@123#asd."; // 从配置文件读取更安全[4](@ref)
    private static final String ISSUER = "your-service";
    private static final long DEFAULT_EXPIRATION = 86400000; // 默认24小时

    // 生成令牌
    public static String createToken(Map<String, Object> claims) {
        return JWT.create()
                .withIssuer(ISSUER)
                .withPayload(claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + DEFAULT_EXPIRATION))
                .sign(Algorithm.HMAC256(SECRET_KEY));
    }

    // 验证并解析令牌返回用户id
    public static Integer validateAndParse(String token) {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET_KEY))
                .withIssuer(ISSUER)
                .build();
        return verifier.verify(token).getClaim("id").asInt();
    }
//    public static DecodedJWT validateAndParse(String token) {
//        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET_KEY))
//                .withIssuer(ISSUER)
//                .build();
//        return verifier.verify(token);
//    }
}